using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace QuickLaunch
{
    public partial class ShortcutForm : Form
    {
        private bool isEditingShortcut = false;

        public ShortcutForm(string shortcut, string target)
        {
            InitializeComponent();

            if (string.IsNullOrEmpty(shortcut))
            {
                this.isEditingShortcut = false;

                // add a shortcut
                this.Text = "Add a Shortcut";

                //this.tb_ShortcutTarget.AcceptsReturn = true;
                this.tb_ShortcutTarget.Text = "Format: \r\n" +
                    "[File Path]\\[File Name] <Parameters>" +
                    "\r\n or \r\n[URL]<?[Param Name]=[Param Value]&...>";
                this.tb_ShortcutTarget.Enter += new System.EventHandler(this.tb_ShortcutTarget_Enter);
            }
            else
            {
                this.isEditingShortcut = true;

                // edit the shortcut
                this.Text = "Edit a Shortcut";

                this.tb_ShortcutKey.Text = shortcut;
                this.shortcutKey = shortcut;
                this.tb_ShortcutTarget.Text = target;
                this.shortcutTarget = target;
            }

            this.tb_ShortcutKey.TextChanged += new System.EventHandler(this.tb_ShortcutKey_TextChanged);
            this.tb_ShortcutTarget.TextChanged += new EventHandler(tb_ShortcutTarget_TextChanged);
        }

        #region Properties

        private string shortcutKey;

        public string ShortcutKey
        {
            get { return this.shortcutKey; }
        }

        private string shortcutTarget;

        public string ShortcutTarget
        {
            get { return this.shortcutTarget; }
        }

        #endregion

        private void tb_ShortcutKey_TextChanged(object sender, EventArgs e)
        {
            this.CheckShortcutKey();
        }

        void tb_ShortcutTarget_TextChanged(object sender, EventArgs e)
        {
            this.tb_ShortcutTarget.Text = this.tb_ShortcutTarget.Text.Replace("\r\n", "");
            this.tb_ShortcutTarget.SelectionStart = this.tb_ShortcutTarget.Text.Length;
        }

        private void tb_ShortcutTarget_Enter(object sender, EventArgs e)
        {
            if (this.tb_ShortcutTarget.Text.StartsWith("Format"))
            {
                this.tb_ShortcutTarget.Text = "";
                this.tb_ShortcutTarget.Enter -= this.tb_ShortcutTarget_Enter;
            }
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            // update the properties.
            this.shortcutKey = this.tb_ShortcutKey.Text;
            if (string.IsNullOrEmpty(this.shortcutKey))
            {
                MessageBox.Show("Please specify the shortcut key");
                this.tb_ShortcutKey.Focus();
                return;
            }
            this.shortcutTarget = this.tb_ShortcutTarget.Text;
            if (string.IsNullOrEmpty(this.shortcutTarget))
            {
                MessageBox.Show("Please specify the shortcut target");
                this.tb_ShortcutTarget.Focus();
                return;
            }

            // write the key - target pair to the 
            // if the key already exists, overwrite the target.
            bool opResult = false;
            if (Program.qlShortcutSetting.GetSettingTable().ContainsKey(shortcutKey))
            {
                opResult = Program.qlShortcutSetting.SetValue(shortcutKey, shortcutTarget);
            }
            // else, add the key - target pair.
            else
            {
                opResult = Program.qlShortcutSetting.Add(shortcutKey, shortcutTarget);
            }
            if (opResult)
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                MessageBox.Show("Some error occurs when add/edit the shortcut.");
            }
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        protected bool CheckShortcutKey()
        {
            // get the shortcut key from the textbox
            string shortcut = this.tb_ShortcutKey.Text;
            // check if the shortcut exists in the setting table
            string target = Program.qlShortcutSetting.GetValue(shortcut);
            if (target != null)
            {
                if (MessageBox.Show(
                    "The shortcut " + shortcut + " has already been assigned to the file: " + target +
                    ". Do you want to overwrite it?", "Confirmation", MessageBoxButtons.YesNo) == DialogResult.No)
                {
                    this.tb_ShortcutKey.Text = "";
                    return false;
                }
            }
            return true;
        }
    }
}